package com.monitor.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName DateUtil
 * @Package com.monitor.common
 * @Description 时间工具类
 * @Author zhixingbai
 * @Create 2018/10/1211:32
 */
public class DateUtil {
    private static final Logger logger=LoggerFactory.getLogger(DateUtil.class);


    private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    public static Date parse(String dateStr) throws ParseException {
        return threadLocal.get().parse(dateStr);
    }

    public static String format(Date date) {
        return threadLocal.get().format(date);
    }

    /**
     * 时间对比
     * @param fromTime
     * @param toTime
     * @param costTime
     * @return
     */
    public static boolean dateDiff(Date fromTime, Date toTime, Integer costTime) {
        long from = fromTime.getTime();
        long to = toTime.getTime();
        int minutes = (int) ((to - from) / (1000 * 60));
        if (minutes >= costTime) {
            return true;
        }
        return false;
    }
}
